package com.stn.zjx.givingtreasure.adapter;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;


import com.amap.api.services.core.PoiItem;
import com.stn.zjx.givingtreasure.R;
import com.stn.zjx.givingtreasure.activity.address.EditAddressActivity;
import com.stn.zjx.givingtreasure.domain.UserAddressDomain;
import com.stn.zjx.givingtreasure.event.LocationEvent;
import com.vondear.rxtools.RxActivityTool;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

public class LocationItemListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{

   public List<PoiItem> dataBeanList=new ArrayList<>();
   private LayoutInflater inflater;
   public Activity context;
   public String province="";
   public String city="";
   public String area="";
   public LocationItemListAdapter(Activity context) {
       inflater = LayoutInflater.from(context);
      this.context=context;
   }

   @Override
   public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
       RecyclerView.ViewHolder vh = new IndexHolder(inflater.inflate(R.layout.item_location_info, parent, false));
       return vh;
   }
   @SuppressLint("SetTextI18n")
   @Override
   public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
                   IndexHolder ih= (IndexHolder) viewHolder;
                  PoiItem poiItem=dataBeanList.get(i);
                   ih.title.setText(poiItem.getTitle());
            ih.info.setText(province+""+poiItem.getCityName()+poiItem.getAdName()+poiItem.getBusinessArea()+poiItem.getTitle());
            ih.rootView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    LocationEvent event=new LocationEvent();
                    event.flag=1;
                     event.province=province;
                     event.city=city;
                    event.area=area;
                    event.address=poiItem.getTitle();
                    event.longitude=poiItem.getLatLonPoint().getLongitude()+"";
                    event.latitude=poiItem.getLatLonPoint().getLatitude()+"";
                    EventBus.getDefault()
                            .post(event);
                    context.finish();
                }
            });
   }

   @Override
   public int getItemCount() {
       return dataBeanList.size();
   }
   public void refreshList(List<PoiItem> dataBeans) {
       dataBeanList.removeAll(dataBeanList);
       dataBeanList.addAll(dataBeans);
       notifyDataSetChanged();
   }

   public void appendList(List<PoiItem> dataBeans) {
       dataBeanList.addAll(dataBeans);
       notifyDataSetChanged();
   }

   public void clearList() {
       dataBeanList.removeAll(this.dataBeanList);
       notifyDataSetChanged();
   }


   public  class IndexHolder extends RecyclerView.ViewHolder{

             public View rootView;
             public TextView title;
             public TextView info;

        IndexHolder(@NonNull View itemView) {
           super(itemView);
           rootView=itemView;
           title=itemView.findViewById(R.id.title);
           info=itemView.findViewById(R.id.info);

        }
   }

}